/* $RCSFile: FAQs.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/FAQs/FAQs.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:53  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Globalization;

using AddonNice.Core;
using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.UI.DataTypes;

using AddonNice.Settings.PageModules.FAQs;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.FAQs
{

    /// <summary>
    /// Initializer class for FAQs 
    /// </summary>
    public class FAQsSI : PortalModuleControlSI
    {
        public FAQsSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FAQsSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FAQsSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FAQsSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

 			HtmlEditorDataType.AddEditorToSettings(bSettings, SettingItemGroup.MODULE_SPECIAL_SETTINGS,pm.ExtendedResourcesGroup);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FAQsSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
    /// FAQ module
    /// </summary>
    public partial class FAQs : PortalModuleControl 
    {
        
        public override Guid GuidID
        {
            get
            {
                return new Guid("{2502DB18-B580-4F90-8CB4-C15E6E531000}");
            }
        }

        ///<summary>
        /// CTor
        ///</summary>
        public FAQs() 
 		{
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Ctor END.",ModuleTraceSwitch.Sw.Info);
         }

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp     =   "FAQS";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new FAQsSI(bSettings,this);
        }

        
        /// <summary>
         /// The Page_Load event on this page calls the BindData() method
         /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Page_Load BEG.",ModuleTraceSwitch.Sw.Info);
            BindData();
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Page_Load END.",ModuleTraceSwitch.Sw.Info);
        }

        /// <summary>
        /// The myDataList_ItemCommand function is used to 
        /// determine the FAQ the user selected in the form
        /// </summary>
        private void myDataList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			// hide answer if shown, show answer if hidden
			if (myDataList.SelectedIndex == e.Item.ItemIndex) 
				myDataList.SelectedIndex = -1;
			else 
				myDataList.SelectedIndex = e.Item.ItemIndex;
            
			BindData();
		}

        /// <summary>
        /// The Binddata method on this User Control is used to
        /// obtain a DataReader of event information from the FAQ
        /// table, and then databind the results to a templated DataList
        /// server control. It uses the AddonNice.FAGsDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        private void BindData() 
        {
            FAQsDB questions = new FAQsDB();
            // CS 8/2004
            try
            {
                if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( "[V]FAQs BindData",ModuleTraceSwitch.Sw.Info);
                DataSet ds = questions.GetFAQsGlobalized(ModuleID);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( "[V]FAQs BindData not null",ModuleTraceSwitch.Sw.Info);
                    try
                    {
                        myDataList.DataSource = ds.Tables[0].DefaultView;              //ds;
                        myDataList.DataBind();
                        
                    }
                    catch (Exception ex)
                    {
                        if (ModuleTraceSwitch.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQs BindData Binding error : {0}", ex),ModuleTraceSwitch.Sw.Info);
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQs BindData ex : {0}", ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
    		if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Render BEG.",ModuleTraceSwitch.Sw.Info);
            try
            {
                base.Render(writer);
				if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine("[V]FAQs Render END.",ModuleTraceSwitch.Sw.Info);
			}
			catch (Exception ex)
			{
				if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQs Render ex :{0}", ex),ModuleTraceSwitch.Sw.Info);
				throw;
			}
        }

        void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item
            && e.Item.ItemType != ListItemType.AlternatingItem
            && e.Item.ItemType != ListItemType.SelectedItem)
                return;
            try
            {
                if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQs myDataList_ItemDataBound  ItemType : {0}", e.Item.ItemType),ModuleTraceSwitch.Sw.Info);

                foreach (Control ctl in e.Item.Controls)
                {
                    if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FAQs myDataList_ItemDataBound  Control : {0}", ctl),ModuleTraceSwitch.Sw.Info);
                    if (ctl is HyperLink)
                    {
                        HyperLink ctl2  =   (HyperLink)ctl;
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( 
                            string.Format("[V]FAQs myDataList_ItemDataBound  SkinId : {0}, NavigateUrl: {1}, ImageUrl: {2}",
                                        ctl2.SkinID,
                                        ctl2.NavigateUrl,
                                        ctl2.ImageUrl
                                        ),ModuleTraceSwitch.Sw.Info);
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( 
                            string.Format("[V]FAQs myDataList_ItemDataBound  ResolveClientUrl : {0}",ResolveClientUrl(ctl2.ImageUrl)),ModuleTraceSwitch.Sw.Info);
                        if ( ctl2.ImageUrl.StartsWith("~") )
                            ctl2.ImageUrl=((HyperLink)ctl).ImageUrl.Substring(1);
                        break;
                       
                    }
                }
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FAQs myDataList_ItemDataBound  error ex : {0}", ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs myDataList_ItemDataBound END.",ModuleTraceSwitch.Sw.Info);
        }
        #region Std Overrides

        public override void Page_PreInit(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Page_PreInit BEG.",ModuleTraceSwitch.Sw.Info);
            this.AddText    =   "FAQS_ADD";
            this.AddUrl     =   Config.ModulesPath +"/FAQs/FAQsEdit.aspx";
            base.Page_PreInit(sender,e);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FAQs Page_PreInit END.",ModuleTraceSwitch.Sw.Info);
        }
        
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            myDataList.ItemCommand      +=  new System.Web.UI.WebControls.DataListCommandEventHandler(this.myDataList_ItemCommand);
            myDataList.ItemDataBound    +=  new DataListItemEventHandler(myDataList_ItemDataBound);
            Load                        +=  new EventHandler(this.Page_Load);
        }

        #endregion

       
    }
}